//
//  ECThemeSpecialSetting.swift
//  EasyCoding
//
//  Created by JY_NEW on 2021/1/9.
//

import Foundation

///可设置颜色表
public protocol ECThemeColorMapped {
    associatedtype ColorType: ECThemeValueType where ColorType.RawValue == UIColor
}
extension ECThemeSetter where ManagerType: ECThemeColorMapped {
    ///设置颜色
    public subscript(dynamicMember kp: WritableKeyPath<TargetType, UIColor?>) -> ManagerType.ColorType? {
        get {
            return self.get(kp)
        }
        nonmutating  set {
            self.set(kp, value: newValue)
        }
    }
    ///设置颜色
    public subscript(dynamicMember kp: WritableKeyPath<TargetType, UIColor>) -> ManagerType.ColorType? {
        get {
            return nil
        }
        nonmutating  set {
            self.set(kp, value: newValue)
        }
    }
    ///设置颜色
    public subscript(dynamicMember kp: WritableKeyPath<TargetType, CGColor?>) -> ManagerType.ColorType? {
        get {
            return nil
        }
        nonmutating  set {
            if let v = newValue {
                self.set { (target) in
                    var t = target
                    t[keyPath: kp] = v.rawValue.cgColor
                }
            }
        }
    }
    ///设置颜色
    public subscript(dynamicMember kp: WritableKeyPath<TargetType, CGColor>) -> ManagerType.ColorType? {
        get {
            return nil
        }
        nonmutating  set {
            if let v = newValue {
                self.set { (target) in
                    var t = target
                    t[keyPath: kp] = v.rawValue.cgColor
                }
            }
        }
    }
}
extension ECThemeSetter where TargetType: UIButton, ManagerType: ECThemeColorMapped {
    ///设置颜色
    public func titleColor(_ color: ManagerType.ColorType, for state: UIControl.State = .normal) {
        self.set { (target) in
            target.setTitleColor(color.rawValue, for: state)
        }
    }
}

///可设置字符串表
public protocol ECThemeStringMapped {
    associatedtype StringType: ECThemeValueType where StringType.RawValue == String
}
extension ECThemeSetter where ManagerType: ECThemeStringMapped {
    ///设置文字
    public subscript(dynamicMember kp: WritableKeyPath<TargetType, String?>) -> ManagerType.StringType? {
        get {
            return self.get(kp)
        }
        nonmutating  set {
            self.set(kp, value: newValue)
        }
    }
    ///设置文字
    public subscript(dynamicMember kp: WritableKeyPath<TargetType, String>) -> ManagerType.StringType? {
        get {
            return self.get(kp)
        }
        nonmutating  set {
            self.set(kp, value: newValue)
        }
    }
}

///可设置图片表
public protocol ECThemeImageMapped {
    associatedtype ImageType: ECThemeValueType where ImageType.RawValue == UIImage?
}
extension ECThemeSetter where ManagerType: ECThemeImageMapped {
    ///设置图片
    public subscript(dynamicMember kp: WritableKeyPath<TargetType, UIImage?>) -> ManagerType.ImageType? {
        get {
            return self.get(kp)
        }
        nonmutating  set {
            self.set(kp, value: newValue)
        }
    }
}
extension ECThemeSetter where TargetType: UIButton, ManagerType: ECThemeImageMapped {
    ///设置图片
    public func backgroundImage(_ image: ManagerType.ImageType, for state: UIControl.State = .normal) {
        self.set { (target) in
            target.setBackgroundImage(image.rawValue, for: state)
        }
    }
}


///可设置字体表
public protocol ECThemeFontMapped {
    associatedtype FontType: ECThemeValueType where FontType.RawValue == UIFont?
}
extension ECThemeSetter where ManagerType: ECThemeFontMapped {
    ///设置字体
    public subscript(dynamicMember kp: WritableKeyPath<TargetType, UIFont?>) -> ManagerType.FontType? {
        get {
            return self.get(kp)
        }
        nonmutating  set {
            self.set(kp, value: newValue)
        }
    }
}
